package com.example.medical_project.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.medical_project.commons.constant.Constants;
import com.example.medical_project.modules.system.domain.DictData;
import com.example.medical_project.modules.system.domain.DictType;
import com.example.medical_project.modules.system.dto.DictTypeDto;
import com.example.medical_project.modules.system.mapper.DictDataMapper;
import com.example.medical_project.modules.system.service.DictTypeService;
import com.example.medical_project.modules.system.mapper.DictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86157
* @description 针对表【sys_dict_type(字典类型表)】的数据库操作Service实现
* @createDate 2025-01-08 16:42:27
*/
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType>
    implements DictTypeService{


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DictDataMapper dictDataMapper;

    @Override
    public Page<DictType> listForPage(DictTypeDto params) {

        Page<DictType> page = new Page<>(params.getPageNum(), params.getPageSize());

        LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StrUtil.isNotBlank(params.getDictName()),DictType::getDictName,params.getDictName());
        queryWrapper.like(StrUtil.isNotBlank(params.getDictType()),DictType::getDictType,params.getDictType());

        this.page(page,queryWrapper);

        return page;


    }

    @Override
    public List<DictType> selectAllDictType() {

        LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(DictType::getStatus, Constants.STATUS_TRUE);

        return this.list(queryWrapper);
    }

    @Override
    public void dictCacheAsync() {

//        查询所有可用字典类型数据
        List<DictType> dictTypes = this.selectAllDictType();

        for (DictType dictType:dictTypes) {
//        根据字典类型数据查询字典数据
            LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DictData::getStatus,Constants.STATUS_TRUE);
            queryWrapper.eq(DictData::getDictType,dictType.getDictType());
            List<DictData> dictData = dictDataMapper.selectList(queryWrapper);

            //方式一：没个字典类型一个key，key存储字典数据
//            //创建RedisKey dict:sys_user_sex
//            String redisKey = Constants.DICT_REDIS_PROFIX+dictType.getDictType();
//            //将数据存储到redis
//            redisTemplate.boundValueOps(redisKey).set(dictData);
            //方式二：使用hash类型，一个key，每个类型做hash的key
            redisTemplate.boundHashOps("dict_data").put(dictType.getDictType(),dictData);
        }

    }
}




